//
//  User.m
//  Eath
//
//  Created by yuhaya on 14-9-8.
//  Copyright (c) 2014年 yuhaya. All rights reserved.
//

#import "User.h"
#import "Tool.h"

@implementation User

+ (BOOL) isLogin
{
    NSUserDefaults * UserDefault = [NSUserDefaults standardUserDefaults];
    NSInteger expires = [UserDefault integerForKey:@"expires"];
   
    NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval date = [dat timeIntervalSince1970];
    
    if((int)date > (int)expires){
        return NO;
    }else{
        return YES;
    }
}

+ (NSString *) token
{
    NSUserDefaults * UserDefault = [NSUserDefaults standardUserDefaults];
    NSString * expires = [UserDefault stringForKey:@"token"];
    return expires;
}

+ (NSDictionary *) loginInfo
{
    if([[self class] isLogin]){
        
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSDictionary * info= [userDefaults dictionaryForKey:@"userinfo"];
        return info;
        
    }else{
        
        return nil;
    }
    
}

+ (Genearch *) loginGen
{
//    DLog(@"%@",[[self class] loginInfo]);
    NSDictionary * dict = [[self class] loginInfo];
    Genearch * gen = [[Genearch alloc] initWithId:dict[@"id"] andName:dict[@"username"] andRelation:@"" andPhoto:@"default_head" andCard:dict[@"card"] andPhone:dict[@"phone"] andMain:YES andSex:[dict[@"sex"] integerValue] andFullName:dict[@"name"] andPwd:@""];
    return gen;
}


+ (void) loginByName:(NSString *)name andPwd:(NSString *)pwd andSuccessBlock:(void(^)())successBlock andFailBlock:(void(^)(NSString *))failBlock
{
    NSDictionary *parameters;
    
    if ([Tool isValidateMobile:name]) {
        parameters = @{@"type":[NSNumber numberWithInt:0],@"phone":name,@"password":pwd};
    }else{
        parameters = @{@"type":[NSNumber numberWithInt:1],@"name": name,@"password":pwd};
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:URL_LOGIN parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * redata = responseObject;
        NSDictionary * data = redata[@"data"];
        NSString * code = redata[@"code"];
        DLog(@"登录接口==%@",redata);
        if(code.intValue  == 0){
            NSUserDefaults * UserDefault = [NSUserDefaults standardUserDefaults];
            
             [UserDefault setObject:data[@"userinfo"] forKey:@"userinfo"];
             [UserDefault setObject:@"123" forKey:@"token"];
//             [UserDefault setObject:data[@"token"] forKey:@"token"];
//             [UserDefault setObject:data[@"expires"] forKey:@"expires"];
             [UserDefault setObject:@"1538469678" forKey:@"expires"];
 
            [UserDefault synchronize];
            successBlock();
            [[self class] sendDeviceToken];
        }else{
            failBlock(redata[@"msg"]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [Tool showMessage:@"登录失败!"];
    }];
}

+(void)sendDeviceToken
{
    NSDictionary * logininfo = [[self class] loginInfo];
    NSString * stuid = logininfo[@"id"];
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    NSString * token = [userdefault stringForKey:@"device_token"];
    
    NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:stuid,@"user_id",token,@"device_token",nil];
    DLog(@"%@",parameters);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString * url = [NSString stringWithFormat:@"%@?token=%@",URL_DEVICE,[[self class] token]];
    DLog(@"%@",url);
    [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * redata = responseObject;
        DLog(@"%@",redata);
//        NSDictionary * data = redata[@"data"];
        NSString * code = redata[@"code"];
        if(code.intValue  == 0){
//            DLog(@"TOKEN SEND OK !");
        }else{
            [Tool showMessage:redata[@"msg"]];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [Tool showMessage:@"token发送失败!"];
    }];
    
    
}

@end
